<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          @click="handleRoot"
        >根目录</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板文件" align="left" prop="fileName" width="390">
        <template slot-scope="scope">
          <a href="#" class="link-type" v-if="scope.row.type=='directory'" @click="toView(scope.row)">{{scope.row.fileName}}</a>
          <div v-if="scope.row.type=='file'">{{scope.row.fileName}}</div>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" width="250"/>
      <el-table-column label="扩展名" align="center" prop="extName" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.type=='directory'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="toView(scope.row)"
          >查看</el-button>
          <el-button
            v-if="scope.row.type=='file'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="toEdit(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改测试单表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="模板目录" prop="path">
          <el-input v-model="form.path" :readonly="true"/>
        </el-form-item>
        <el-form-item label="模板文件" prop="fileName">
          <el-input v-model="form.fileName" :readonly="true"/>
        </el-form-item>
        <el-form-item label="模板内容" prop="content">
          <el-input
            type="textarea"
            :rows="20"
            style="width: 100%"
            v-model="form.content">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getIndex,getToView,getToEdit,getToSave} from "@/api/cms/template";

export default {
  name: "template",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getAll();
  },
  methods: {
    /** 查询公告列表 */
    getAll() {
      this.loading = true;
      getIndex().then(response => {
        this.templateList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getAll();
    },
    handleRoot() {
      this.getAll();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    toEdit(row) {
      this.reset();
      getToEdit(row).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模板";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      getToSave(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getAll();
      });
    },
    toView(row) {
      console.log(row);
      getToView(row).then(response=>{
        this.templateList = response.data;
      })
    }
  }
}
</script>

<style scoped>
.link-type{
  color: #3c8dbc;
}
</style>
